<?php
/**
 * 用户登陆检验类
 * @author cenchy
 */
class UserAuth
{
	private $loginModule = array('User-UserLogin');
	private $snkey;
	private $cookiename;
	private $UserId = 0;
	private $isLogin = false;
	private $isloginModule = false;//公共页面,无需登陆
	/**用户model**/
	private $userM;

	public function __construct()
	{
		$this->snkey = C('COOKIE_LOGIN_SNKEY');
		$this->cookiename = C('COOKIE_LOGIN_NAME');
		$this->userM = D('User');
		$this->checkLogin();
	}

	private function checkLogin(){
		$module = '';
		if(defined('GROUP_NAME')){
			$module = GROUP_NAME.'-';
		}
		$module .= MODULE_NAME;
		if(in_array($module,$this->loginModule)){
			//登陆相关链接
			$this->isloginModule=true;
		}

		$userCookie = cookie($this->cookiename);

		$userCookieArray=explode('|',$userCookie);
		$userip = get_client_ip();
		$UserId = intval($userCookieArray[0]);
		if(!($UserId&&isset($userCookieArray[1])&&isset($userCookieArray[2]))){
			$this->isLogin=false;
			return $this->isLogin;
		}
		$UserObject = $this->userM->getObject($UserId);
		if($UserObject['LoginKey']==$userCookieArray[2]){
			$this->UserId=$UserId;
			$this->isLogin=true;
		}else{
			$this->isLogin=false;
		}
		return $this->isLogin;
	}

	public function isLogin(){
		return $this->isLogin;
	}

	public function isloginModule(){
		return $this->isloginModule;
	}

	public function getUserId(){
		return $this->UserId;
	}


}//类定义结束
?>
